from appium import webdriver
from common.yaml_config import GetConf


class CommonDriver:
    def __init__(self):
        """
        CommonDriver类初始化，创建一个空的WebDriver实例。
        """
        self.driver = None
        self.config = GetConf().get_plat_info()  # 读取配置一次并保存

    def get_driver(self):
        """
        获取WebDriver实例并进行初始化。
        :return: WebDriver实例
        """
        # 从配置中提取所需的信息
        desc = {
            'platformName': self.config['platformName'],
            'platformVersion': self.config['platformVersion'],
            'appPackage': self.config['appPackage'],
            'appActivity': self.config['appActivity'],
            'noReset': self.config['noReset'],
            'unicodeKeyboard': self.config['unicodeKeyboard'],
            'udid': self.config['udid'],
        }

        # 定义命令执行器的URL
        command_executor_url = 'http://127.0.0.1:4723/wd/hub'
        # 创建WebDriver实例
        self.driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)

        return self.driver
